<template>
  <el-main class="bg table_wrap coupon">
    <!-- table头部检索 -->
    <div class="div-form">
      <el-form
        ref="elFormRef"
        :model="formInline"
        label-width="90px"
        :style="{ width: '100%' }"
        label-position="left"
      >
        <el-row :gutter="20">
          <el-col :span="5">
            <el-form-item label="优惠券名称">
              <el-input v-model="formInline.coupon_name"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <baseTable
      :tableColumns="columns"
      :requestApi="url_get_list"
      :isShowSelect="true"
      :isShowOptColumn="true"
      :imgflag="false"
      :vm='vm'
      v-model:isRefresh="isRefresh"
      :otherParams="formInline"
      @handleSelectionChange="handleSelectionChange"
      @handleCurrentChangeNew="handleCurrentChangeNew"
    >
      <!-- 头部按钮 -->
      <template #baseHeadFunt>
        <el-button type="primary" @click="onSubmit">查询</el-button>
        <el-button @click="onReset(true)">重置</el-button>
        <el-button
          type="danger"
          v-if="user_group == '管理员' || $check_action('/order/table', 'del')"
          class="search_btn_del"
          @click="delInfo()"
          >删除</el-button
        >
        <!-- <el-button
          v-if="
            user_group == '管理员' || $check_option('/goods/table', 'seller')
          "
          class="float-right search_btn_add"
          @click="router.push('./view?')"
          >添加</el-button
        > -->
      </template>
      <!-- table内操作 -->
      <template #baseTableFunt="{ scope }">
        <el-button
          type="primary"
          size="small"
          @click="router.push('./view?' + field + '=' + scope.row.coupon_id)"
          >详情</el-button
        >
      </template>
      <template #baseTableFunt2="{ scope }">
        <el-button
          type="primary"
          size="small"
          @click="router.push('./view?' + field + '=' + scope.coupon_id)"
          >详情</el-button
        >
      </template>
      <!-- 特殊表格内容 -->
      <template #tableContent="{ scope, column }">
        <!-- <el-image
         :preview-teleported="true"
          v-if="column.prop === 'img'"
          style="width: 100px; height: 100px"
          :src="$fullUrl($banseUrl, scope.row.img)"
          :preview-src-list="[$fullUrl($banseUrl, scope.row.img)]"
        >
        </el-image> -->
        <span v-if="column.prop === 'coupon_user_id'">
          {{getObj(list_user, scope.row.coupon_user_id).nickname}}
        </span>
        <span v-if="column.prop === 'coupon_price'">
          满
          {{ scope.row["coupon_price"] }}
          减
          {{ scope.row["coupon_price1"] }}
        </span>
        <span v-if="column.prop === 'coupon_time'">
          {{ $toTime(scope.row["coupon_time"].split(",")[0], "YYYY-MM-DD HH:mm:ss") }}
          到
          {{ $toTime(scope.row["coupon_time"].split(",")[1], "YYYY-MM-DD HH:mm:ss") }}
        </span>
        <span v-if="column.prop === 'create_time'">
          {{ $toTime(scope.row["create_time"], "YYYY-MM-DD HH:mm:ss") }}
        </span>
        <span v-if="column.prop === 'login_time'">
          {{ $toTime(scope.row["login_time"], "YYYY-MM-DD HH:mm:ss") }}
        </span>
        <span v-if="column.prop === 'update_time'">
        {{ $toTime(scope.row["update_time"], "YYYY-MM-DD HH:mm:ss") }}
        </span>
      </template>
      <template #tableContent2="{ scope, column }"> 
        <span v-if="column.prop === 'coupon_price'">
          {{ column.label }}：
          {{getObj(list_user, scope.coupon_user_id).nickname}}
        </span>
        <span v-if="column.prop === 'coupon_price'">
          {{ column.label }}：满
          {{ scope["coupon_price"] }}
          减
          {{ scope["coupon_price1"] }}
        </span>
        <span v-if="column.prop === 'coupon_time'">
          {{ column.label }}： {{ $toTime(scope["coupon_time"].split(",")[0], "YYYY-MM-DD HH:mm:ss") }}
          到
          {{ $toTime(scope["coupon_time"].split(",")[1], "YYYY-MM-DD HH:mm:ss") }}
        </span>
        <span v-if="column.prop === 'create_time'">
          {{ column.label }}： {{ $toTime(scope["create_time"], "YYYY-MM-DD HH:mm:ss") }}
        </span>
        <span v-if="column.prop === 'update_time'">
          {{ column.label }}： {{ $toTime(scope["update_time"], "YYYY-MM-DD HH:mm:ss") }}
        </span>
        <span v-if="column.prop === 'login_time'">
          {{ column.label }}： {{ $toTime(scope["login_time"], "YYYY-MM-DD HH:mm:ss") }}
        </span>
      </template>
    </baseTable>
  </el-main>
</template>
<script setup>
import { useRouter } from "vue-router";
let router = useRouter();
import {
  $toTime,
  $check_option,
  $fullUrl,
  formatDate,
  $banseUrl,
  $check_action,
} from "@/utils/utils.js";
import { $post, $get } from "@/api/Api.js";
import { ElMessage } from "element-plus";
import { ref, toRefs, computed, onMounted } from "vue";
import storageHelper from "@/libs/StorageHelper";
let table = ref("coupon");
let field = ref("coupon_id");
let url_get_list = ref("/api/coupon/get_list");
let url_del = ref("/api/coupon/del?");
let user_group = storageHelper.getUserInfo().user_group;
let user_id = storageHelper.getUserInfo().user_id;
// 列名
const columns = [
  { prop: "coupon_name", label: "优惠券名称" },
  { prop: "coupon_user_id", label: "所属卖家ID" },
  { prop: "coupon_type", label: "优惠券金额" },
  { prop: "coupon_name", label: "优惠券类型" },
  { prop: "coupon_time", label: "有效期" },
  { prop: "create_time", label: "创建时间" },
  { prop: "update_time", label: "更新时间" },
];
// 刷新table
let isRefresh = ref(false);
let vm=ref({
    id: "coupon_id",
})
//批量勾选数据
let selectList = ref([]);
let list_user = ref([]);
//点击当前行数据
let currentRowTableProduct = ref({});
//表格勾选回调
let handleSelectionChange = (val) => {
  selectList.value = val.map((item) => {
    return item.coupon_id;
  });
};
//点击当前行
let handleCurrentChangeNew = (val) => {
  currentRowTableProduct.value = val;
};
// 批量删除
let delInfo = () => {
  ElMessageBox.confirm("此操作将永久删除该文件, 是否继续?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      selectList.value.map((item) => {
        delFun(item);
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消操作",
      });
    });
};

// 调用删除方法
let delFun = (id) => {
  $get(url_del.value + "coupon_id=" + id).then((json) => {
    if (json.result) {
      ElMessage.success("删除成功");
    } else if (json.error) {
      ElMessage.error(json.error);
    }
    isRefresh.value = true;
  });
};
//获取用户
let get_list_user = async () => {
  var json = await $get("/api/user/get_list");
  if (json.result) {
    list_user.value = json.result.list;
  } else if (json.error) {
    console.error(json.error);
  }
};
// 查询数组对象某个是否包含某个值,返回对应对象
let getObj = (arr, val) => {
  let obj = arr.find((item) => item.user_id == val);
  if (obj) {
    return obj;
  } else {
    return "-";
  }
};
let get_list_before = () => {
  if (user_group !== "管理员" && $check_option("/goods/table", "seller")) {
    param["coupon_user_id"] = user_id;
  }
};
let table_class = ({ row, column, rowIndex, columnIndex }) => {
  return "table_class";
};
// 头部搜索// 搜索
let formInline = ref({
  coupon_name: "",
  coupon_user_id: "",
});
// 搜索
let onSubmit = () => {
  isRefresh.value = true;
};

// 重置
let onReset = (refresh) => {
  formInline.value = {
    coupon_name: "",
    coupon_user_id: "",
  };
  isRefresh.value = refresh;
};

onMounted(() => {
  get_list_user()
  if (user_group === "卖家") {
    forminline.value.coupon_user_id = user_id;
  }
  if (user_group !== "管理员" && $check_option("/goods/table", "seller")) {
    forminline.value.coupon_user_id = user_id;
  }
});
</script>
<style lang="less" scoped>
.div-form {
  padding: 10px;
}
</style>
